package com.yike.user.controller;

import com.yike.common.api.ApiResult;
import com.yike.common.base.BaseController;
import com.yike.common.enums.OperationLogType;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.pagination.Paging;
import com.yike.user.param.MachineParam;
import com.yike.user.param.PlatformMachineInfoParam;
import com.yike.user.param.PlatformMachinePageParam;
import com.yike.user.service.PlatformMachineService;
import com.yike.user.vo.PlatformMachineInfoVo;
import com.yike.user.vo.PlatformMachineVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 平台设备 控制器
 *
 * @author Sakura
 * @since 2024-11-07
 */
@Slf4j
@RestController
@RequestMapping("/platform/machine")
@Module("user")
@Api(value = "平台设备API", tags = {"平台设备"})
public class PlatformMachineController extends BaseController {

    @Autowired
    private PlatformMachineService platformMachineService;

    @PostMapping("/add")
    @OperationLog(name = "添加设备", type = OperationLogType.ADD)
    @ApiOperation(value = "添加设备", response = ApiResult.class)
    public ApiResult<Boolean> addMachine(@Validated @RequestBody MachineParam machineParam) throws Exception {
        boolean flag = platformMachineService.addMachine(machineParam);
        return ApiResult.result(flag);
    }

    @PostMapping("/update")
    @OperationLog(name = "修改设备", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改设备", response = ApiResult.class)
    public ApiResult<Boolean> updateMachine(@Validated @RequestBody MachineParam machineParam) throws Exception {
        boolean flag = platformMachineService.updateMachine(machineParam);
        return ApiResult.result(flag);
    }

    @GetMapping("exportTemplate")
    @ApiOperation(value = "导出模板")
    public void exportTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        platformMachineService.exportTemplate(request, response);
    }

    @PostMapping("importMachine")
    @ApiOperation(value = "导入设备", response = ApiResult.class)
    public ApiResult<Boolean> importMachine(@RequestParam(value = "file") MultipartFile file) throws Exception {
        boolean flag = platformMachineService.importMachine(file);
        return ApiResult.result(flag);
    }

    @PostMapping("/getPageList")
    //@OperationLog(name = "设备列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "设备列表", response = PlatformMachineVo.class)
    public ApiResult<Paging<PlatformMachineVo>> getPlatformMachineList(@Validated @RequestBody PlatformMachinePageParam platformMachinePageParam) throws Exception {
        Paging<PlatformMachineVo> paging = platformMachineService.getPlatformMachineList(platformMachinePageParam);
        return ApiResult.ok(paging);
    }

    @PostMapping("/getInfo")
    //@OperationLog(name = "设备列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "设备编辑详情", response = PlatformMachineInfoVo.class)
    public ApiResult<PlatformMachineInfoVo> getInfo(@Validated @RequestBody PlatformMachineInfoParam platformMachineInfoParam) throws Exception {
        PlatformMachineInfoVo info = platformMachineService.getInfo(platformMachineInfoParam);
        return ApiResult.ok(info);
    }

    /**
     * 设备详情
     */
    @GetMapping("/info/{id}")
    //@OperationLog(name = "设备详情", type = OperationLogType.INFO)
    @ApiOperation(value = "设备详情", response = PlatformMachineVo.class)
    public ApiResult<PlatformMachineVo> getPlatformMachine(@PathVariable("id") Long id) throws Exception {
        PlatformMachineVo platformMachineVo = platformMachineService.getPlatformMachine(id);
        return ApiResult.ok(platformMachineVo);
    }

    @GetMapping("/unbind/{id}")
    @OperationLog(name = "解绑", type = OperationLogType.INFO)
    @ApiOperation(value = "解绑", response = ApiResult.class)
    public ApiResult<Boolean> unbind(@PathVariable("id") Long id) throws Exception {
        boolean flag = platformMachineService.unbind(id);
        return ApiResult.result(flag);
    }

    @GetMapping("/delete/{id}")
    @OperationLog(name = "删除", type = OperationLogType.INFO)
    @ApiOperation(value = "删除", response = ApiResult.class)
    public ApiResult<Boolean> delete(@PathVariable("id") Long id) throws Exception {
        boolean flag = platformMachineService.delete(id);
        return ApiResult.result(flag);
    }

}

